$(function () {
    G.logic.map = {
        op: {
            lat: lat,
            lng: lng,
            adr: initaddress,
            type: "baidu"
        },
        /*op: {
            lat: 31.308563,
            lng: 121.525808,
            adr: "上海市杨浦区政益路28号",
            type: "tencent"
        },*/
        el: {
            lat: $("#lat"),
            lng: $("#lng"),
            suggestId: $("#suggestId"),
            suggestId1: $("#suggestId1")
        },
        maps: null,
        is_default: false,
        is_change: false,
        baidu: {
            point: null,
            marker: null,
            infowindow: null,
            map: null,
            load: function () {
                if (typeof (BMap) == 'undefined') {
                    var script = document.createElement("script");
                    script.src = "https://api.map.baidu.com/api?v=2.0&ak=359042E5AC9ced07c553ebe2042aad73&callback=G.logic.map.baidu.init";
                    document.body.appendChild(script);
                } else {
                    this.init();
                }

            },
            init: function (op) {
                var self = this;
                self.map = new BMap.Map("l-map");
                self.point = new BMap.Point(G.logic.map.op.lng, G.logic.map.op.lat);
                self.map.centerAndZoom(self.point, 15);
                self.map.enableScrollWheelZoom();
                self.map.enableScrollWheelZoom();

                self.marker = new BMap.Marker(self.point);
                self.map.addOverlay(self.marker);



                self.map.addEventListener("dragend", function showInfo() {
                    self.marker.closeInfoWindow();
                    self.showAddress(self.marker);
                });

                self.map.addEventListener("dragging", function showInfo() {
                    var cp = self.map.getCenter();
                    self.marker.setPosition(new BMap.Point(cp.lng, cp.lat));

                });
                self.marker.enableDragging();
                self.marker.addEventListener("click", function (e) {
                    this.openInfoWindow(self.infoWindow);
                });
                self.marker.addEventListener("dragend", function (e) {
                    self.marker.closeInfoWindow();
                    self.showAddress(self.marker);
                });

                var opts = { width: 220, height: 80, title: "原本位置" };
                self.infoWindow = new BMap.InfoWindow(" " + G.logic.map.op.adr + " ,拖拽地图或红点修改位置!你也可以直接修改上方位置系统自动定位!", opts);  // 创建信息窗口对象
                if (!G.logic.map.is_change) {
                    self.marker.openInfoWindow(self.infoWindow);
                } else {
                    self.search();
                }
                if (G.logic.map.is_default) {
                    window.setTimeout(function () {
                        self.locate();
                    }, 1000);
                }

            },
            locate: function () {
                var self = this;
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function (r) {
                    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                        point = new BMap.Point(r.point.lng, r.point.lat);
                        self.marker.setPosition(point);
                        self.map.panTo(point);
                        var opts = { width: 220, height: 60, title: "定位成功" };
                        self.infoWindow = new BMap.InfoWindow("这是你当前的位置!,移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!", opts);  // 创建信息窗口对象
                        self.marker.openInfoWindow(self.infoWindow);
                        self.showAddress(self.marker, true);

                    } else {
                        console.log("无法获取定位");
                    }
                });
            },
            search: function () {
                var self = this;
                var myGeo = new BMap.Geocoder();
                var addr = G.logic.map.el.suggestId.val();
                addr = addr.length > 0 ? addr : G.logic.map.op.adr;
                myGeo.getPoint(addr, function (point) {
                    if (point) {
                        self.marker.setPosition(point);
                        self.map.panTo(point);
                        var opts = { width: 220, height: 60, title: "搜索位置" };
                        self.infoWindow = new BMap.InfoWindow("" + addr + ",移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!", opts);  // 创建信息窗口对象
                        self.marker.openInfoWindow(self.infoWindow);      // 打开信息窗口
                        // map.centerAndZoom(self.marker.getPosition(), self.map.getZoom());
                        self.showAddress(self.marker, true);
                    } else {
                        console.log("Search no point");
                    }
                }, "全国");
            },
            showAddress: function (marker, callout) {
                var latlng = marker.getPosition();
                var self = this;
                var myGeo = new BMap.Geocoder();
                myGeo.getLocation(latlng, function (result) {
                    if (result) {
                        document.getElementById('suggestId').value = result.address;
                        var el = G.logic.map.el;
                        el.lat.val(latlng.lat);
                        el.lng.val(latlng.lng);

                        var streetNumber=result.addressComponents.streetNumber;
                        var street=result.addressComponents.street;
                        if(streetNumber!="" && streetNumber!=undefined){
                            el.suggestId.val(streetNumber);
                            el.suggestId1.val(streetNumber);
                        }else{
                            el.suggestId.val(street);
                            el.suggestId1.val(street);
                        }
                        //el.suggestId.val(result.address);
                        if (!callout) {
                            var opts = { width: 220, height: 80, title: "标注位置" };
                            self.infoWindow = new BMap.InfoWindow(" " + result.address + " ,拖拽地图或红点修改位置!你也可以直接修改上方位置系统自动定位!", opts);
                            //self.marker.openInfoWindow(self.infoWindow);
                        }

                    } else {
                        console.log("无法获取定位");
                    }
                });
            }

        },

        tencent: {
            point: null,
            marker: null,
            infowindow: null,
            map: null,
            geocoder: null,
            load: function () {
                if (typeof (qq) == 'undefined') {
                    var script = document.createElement("script");
                    script.src = "https://map.qq.com/api/js?v=2.exp&key=JZSBZ-ZI4RF-HRCJY-JN3OW-V37KS-SOB4K&callback=G.logic.map.tencent.init";
                    //alert(script);
                    document.body.appendChild(script);
                } else {
                    this.init();
                }

            },
            init: function (op) {
                var self = this;
                self.point = new qq.maps.LatLng(G.logic.map.op.lat, G.logic.map.op.lng);
                self.map = new qq.maps.Map(document.getElementById("l-map"), {
                    center: self.point,
                    zoom: 15
                });

                self.infowindow = new qq.maps.InfoWindow({
                    map: self.map,
                    position: self.point,
                    content: " <div style='width:200px'>原本位置 {0} ,拖拽地图或红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(G.logic.map.op.adr)
                });
                self.marker = new qq.maps.Marker({
                    position: self.point,
                    draggable: false,
                    map: self.map
                });
                self.marker.disableDragging();
                qq.maps.event.addListener(self.map, 'drag', function () {
                    var p = self.map.getCenter();
                    self.marker.setPosition(p);

                });
                qq.maps.event.addListener(self.map, 'dragend', function () {
                    var p = self.map.getCenter();
                    self.infowindow.close();
                    self.showAddress(p);
                });
                qq.maps.event.addListener(self.marker, 'dragend', function () {
                    var p = self.marker.getPosition();
                    self.showAddress(p);
                    self.infowindow.close();

                });
                qq.maps.event.addListener(self.marker, 'click', function () {
                    self.infowindow.open();
                });
                if (self.geocoder == null) {
                    self.geocoder = new qq.maps.Geocoder();
                }
                ///同步infowindow
                qq.maps.event.addListener(self.marker, 'position_changed', function () {
                    var p = self.marker.getPosition();
                    self.infowindow.setPosition(p);
                });

                if (!G.logic.map.is_change) {
                    self.infowindow.open();
                } else {
                    self.search();
                }
                if (G.logic.map.is_default) {
                    window.setTimeout(function () {
                        self.locate();
                    }, 1000);
                }

            },

            setval: function (result) {
                var self = this;
                var p = result.detail.location, addr = result.detail.address;
                //JSON.parse(result.detail);
                //console.log(result);
                console.log(result.detail);
                var streetNumber=result.detail.addressComponents.streetNumber;
                var street=result.detail.addressComponents.street;

                self.marker.setPosition(p);
                self.map.panTo(p);
                var el = G.logic.map.el;
                el.lat.val(p.lat);
                el.lng.val(p.lng);

                if(streetNumber!="" && streetNumber!=undefined){
                    el.suggestId.val(streetNumber);
                    el.suggestId1.val(streetNumber);
                }else{
                    el.suggestId.val(street);
                    el.suggestId1.val(street);
                }

            },
            locate: function () {
                var self = this;
                navigator.geolocation.getCurrentPosition(function (position) {
                    var p = new qq.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    self.map.panTo(p)
                    self.marker.setPosition(p);
                    self.infowindow.setContent("<div style='width:200px'>定位成功这是你当前的位置!,移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!</div>");
                    self.infowindow.open();
                    self.rgeocoder().getAddress(p);
                });

            },
            search: function () {
                var self = this;
                var addr = G.logic.map.el.suggestId.val();
                addr = addr.length > 0 ? addr : G.logic.map.op.adr;
                self.geocoder.getLocation(addr);
                self.geocoder.setComplete(function (result) {
                    self.setval(result);
                    self.infowindow.open();
                    self.infowindow.setContent("<div style='width:200px'>搜索位置 {0} ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(result.detail.address))
                })

            },
            showAddress: function (p) {
                var self = this;
                self.geocoder.getAddress(p);
                self.geocoder.setComplete(function (result) {
                    self.setval(result);
                    self.infowindow.setContent("<div style='width:200px'>标注位置 {0} ,移动红点修改位置!你也可以直接修改上方位置系统自动定位!</div>".format(result.detail.address))
                })
            }

        },
        _type: function () {
            // return $(":radio[name='map_type']:checked").data("mapty");
            return $("select[name='map_type'] option:selected").data("mapty");
        },
        init: function (op) {
            var self = this;
            if (op) { self.op = op; self.is_default = false; };
            self.op.type = self._type();
            self.maps = this[this.op.type];
            self.maps.load();
            var positioning = $("#positioning");
            positioning.click(function () {
                self.maps.search();
            });
            // $(":radio[name='map_type']").on("click", function (e) {
            //     G.logic.map.is_change = true;
            //     self.op.type = self._type();
            //     self.maps = self[self.op.type];
            //     $("#l-map").css("background-color", "white").html("<div class='loading'>地图加载中...</div>")
            //     self.maps.load();
            // })
            $("select[name='map_type']").on("change", function (e) {
                G.logic.map.is_change = true;
                self.op.type = self._type();
                self.maps = self[self.op.type];
                $("#l-map").css("background-color", "white").html("<div class='loading'>地图加载中...</div>")
                self.maps.load();
            })
        }
    }
})